<%@page import = "sanger.Utilities" %>
<%
            if (Utilities.isLogged(session)) {
                response.sendRedirect(request.getContextPath() + "/index.jsp");
            }


%>


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<head>
<title>Simple filestore</title>




<body>
    
        <h1><a href="index.jsp">Simple File Store</a></h1>
  

   
        <h1>Login page</h1>
        <form method="post" action="login.jsp">
				Username:<br /><input type="text" name="username" /><br />
				Password:<br /><input type="password" name="password" /><br />
            <input type="submit" value="Login" /><br />
            <p></p>
        </form>

        <%
                    String username = request.getParameter("username");
                    String pass = request.getParameter("password");
                    String context = request.getContextPath();

                    if (username != null && pass != null) {
                        if (!username.isEmpty()
                                && !pass.isEmpty()) {

                            

                            boolean authenticated = Utilities.authenticate(username, pass);
                            //auth success
                            if (authenticated) {

                                
                                session.setAttribute("uname", username);
                                session.setAttribute("auth", "logged");
                                

                                response.sendRedirect(context + "/index.jsp");
                            }
                            //auth fails
                            if (!authenticated) {
        %><span style="font-weight: bold; color: red">Wrong credentials!</span><%                                            }
                                    } else {
        %><span style="font-weight: bold; color: red">Please enter your username and password.</span><%                                 }
                             }

        %>

    

    
</body>
</html>
